package com.hxzy.controll;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hxzy.domain.Contents;
import com.hxzy.domain.Students;
import com.hxzy.service.ContentService;
import com.hxzy.service.StudentService;

@RestController
public class Controll {
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private ContentService contentService;
	
	@RequestMapping("/getStudents.do")
	public String getStudents(HttpServletRequest req,
			HttpServletResponse rep) throws UnsupportedEncodingException{
		
		req.setCharacterEncoding("UTF-8");
		rep.setContentType("text/html");
		rep.setCharacterEncoding("UTF-8");
		
		List<Students> stu=studentService.getStudents();
		String st="[";
		for (Students s : stu) {
			st+="{\"id\":"+s.getId()+","+"\"name\":\""+s.getName()+"\","+"\"age\":"+s.getAge()+","
					+"\"sex\":\""+s.getSex()+"\","+"\"address\":\""+s.getAddress()+"\"},";
		}
		String sr=st.substring(0, st.length()-1);
		System.out.println(st);
		sr+="]";
		System.out.println(sr);
		return sr ;
	}
	
	@RequestMapping("/getContents.do")
	public String getContents(HttpServletRequest req,
			HttpServletResponse rep) throws UnsupportedEncodingException{
		
		req.setCharacterEncoding("UTF-8");
		rep.setContentType("text/html");
		rep.setCharacterEncoding("UTF-8");
		
		List<Contents> cons=contentService.getContents();
		String st="[";
		for (Contents c : cons) {
			st+="{\"id\":"+c.getId()+","+"\"item\":\""+c.getItem()+"\"},";
		}
		String sr=st.substring(0, st.length()-1);
		System.out.println(st);
		sr+="]";
		System.out.println(sr);
		return sr ;
	}
	
}
